//
//  XMDynamicViewController.m
//  XunMi
//
//  Created by MacBook on 2024/3/6.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMOtherDynamicViewController.h"
#import "XMOtherDynamicTableViewCell.h"
#import "XMDynamicFilterView.h"
#import "XMDynamicModel.h"
#import "XMUserDetailViewController.h"

@interface XMOtherDynamicViewController () <XMOtherDynamicTableViewCellDelegate>
@property (nonatomic, strong) NSMutableArray <XMDynamicModel *> *dataArray;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, copy) NSString *prevIndex;
@end

@implementation XMOtherDynamicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVariables];
    [self setupSubviews];
    [self dynamicListData];
    [self setupRefresh];
}

#pragma mark - 初始化变量
- (void)initVariables {
    self.page = 1;
    self.prevIndex = @"0";
    [kNotificationCenter addObserver:self selector:@selector(dynamic_refresh) name:@"dynamic_refresh" object:nil];
}

#pragma mark - refresh
- (void)setupRefresh {
    XMWeakSelf(self);
    self.tableView.mj_header = [XMRefreshNormalHeader headerWithRefreshingBlock:^{
        XMStrongSelf(self);
        [self dynamicListDataRefresh];
    }];
    self.tableView.mj_footer = [XMRefreshBackFooter footerWithRefreshingBlock:^{
        XMStrongSelf(self);
        self.page += 1;
        [self dynamicListData];
    }];
    self.tableView.mj_footer.hidden = YES;
}

- (void)dynamicListDataRefresh {
    self.page = 1;
    self.prevIndex = @"0";
    [self.dataArray removeAllObjects];
    [self dynamicListData];
}

- (void)dynamicListData {
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{
        @"page_no": @(self.page),
        @"page_size": @(10),
        @"page_index": self.prevIndex
    }.mutableCopy;
    
    if (![XMUtil isBlankString:self.userModel.user_id]) {
        [dic setObject:self.userModel.user_id forKey:@"see_user_id"];
    }
    
    [YHNetworkManager.sharedInstance postWithUrl:@"api/dynamic/obtain/user" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        [self.tableView hideStatusView];
        self.prevIndex = tip.prevIndex;
        if (tip.success) {
            [MBProgressHUD hideHUD];
            NSArray *record = [result safe_objectForKey:@"record"];
            NSArray *array = [XMDynamicModel mj_objectArrayWithKeyValuesArray:record];
            [self.dataArray addObjectsFromArray:array];
            if (self.dataArray.count == 0) {
                [self.tableView showStatusView:(YHViewStatusTypeEmptyData)];
                self.tableView.statusTitleLabel.text = @"还没有动态哦~";
            }
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_footer.hidden = self.dataArray.count == 0;
        if (tip.loadMore) {
            [self.tableView.mj_footer endRefreshing];
        } else {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

#pragma mark - setupSubviews
- (void)setupSubviews {
    [self.navLeft2Btn setTitle:kStringFormat(@"%@·动态", self.userModel.showName) forState:UIControlStateNormal];
    
    [self.navRightBtn setImage:kGetImage(@"dt_hi") forState:UIControlStateNormal];
    [self.navRightBtn addTarget:self action:@selector(chat) forControlEvents:UIControlEventTouchUpInside];
    
    [self.tableView registerNib:kNibWithName(kStringFromClass(XMOtherDynamicTableViewCell)) forCellReuseIdentifier:kStringFromClass(XMOtherDynamicTableViewCell)];
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(kNavBarAndStatusBarHeight, 0, 0, 0));
    }];
}

- (void)chat {
    
}
#pragma mark - Actions
- (void)dynamic_refresh {
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMOtherDynamicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kStringFromClass(XMOtherDynamicTableViewCell)];
    XMDynamicModel *model = [self.dataArray safe_objectAtIndex:indexPath.row];
    cell.model = model;
    cell.indexPath = indexPath;
    cell.delegate = self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMDynamicModel *model = [self.dataArray safe_objectAtIndex:indexPath.row];
    return model.otherCellH;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

#pragma mark - XMOtherDynamicTableViewCellDelegate

- (void)handelUserDetail:(XMDynamicModel *)model {
    XMUserDetailViewController *vc = [[XMUserDetailViewController alloc] init];
    vc.userId = [model.user_id isEqualToString:kUserModel.user_id] ? @"" : model.user_id;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)handelMore:(XMDynamicModel *)model cell:(nonnull XMOtherDynamicTableViewCell *)cell {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    XMDynamicModel *dynamicModel = self.dataArray[indexPath.row];
    dynamicModel.isOpening = !dynamicModel.isOpening;
    [self reloadRowsAtIndexPaths:@[indexPath]];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:(UITableViewScrollPositionTop)animated:YES];
}

- (void)handelPraise:(XMDynamicModel *)model praiseButton:(UIButton *)praiseButton indexPath:(nonnull NSIndexPath *)indexPath {
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{
        @"type": @(2),
        @"index": model.xmId
    }.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/dynamic/update" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            [MBProgressHUD hideHUD];
            if (praiseButton.selected) {
                model.is_like = NO;
                model.likes -= 1;
            } else {
                model.is_like = YES;
                model.likes += 1;
            }
            [self reloadRowsAtIndexPaths:@[indexPath]];
        }
    }];
}

- (void)handelfShare:(XMDynamicModel *)model shareButton:(UIButton *)shareButton indexPath:(nonnull NSIndexPath *)indexPath {
    
}

#pragma mark - lazy
- (NSMutableArray<XMDynamicModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)dealloc {
    [kNotificationCenter removeObserver:self];
    NSLog(@"释放了%@",[self class]);
}

@end
